<template>
  <div class="upward">
    <img src="../assets/路径.png" alt="" @click="goback" />
    <span class="text">位置选择</span>
  </div>
  <div class="content">
    <div class="box">
      <span>
        <img class="green" src="../assets/ic_origin.png" alt="" />
        <!-- 展示startCity的值 -->
        <span>{{ cityStore.startCity }}</span>
        <img class="tri" src="../assets/矩形.png" alt="" @click="showCity" />
      </span>
      <van-field placeholder="你要从哪儿上车" v-model="searchKeyword" />
    </div>
  </div>
  <div class="down">
    <div class="area1">
      <!-- 遍历placeData并显示 -->
      <div
        v-for="item in cityStore.placeData"
        :key="item.id"
        class="downflex"
        @click="selectPlace(item)"
      >
        <img src="../assets/编组 5(1).png" alt="" />
        <div class="downtext">
          <div class="title">{{ item.name }}</div>
          <div class="gray">{{ item.address }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCityStore } from '@/stores/cityStore'

const router = useRouter()
const cityStore = useCityStore()
const searchKeyword = ref('')

function showCity() {
  router.push('/cities')
}

// 监听searchKeyword的变化，并调用接口
watch(searchKeyword, async (newVal) => {
  if (newVal) {
    await cityStore.fetchPlacesByKeyword(newVal)
  }
})

function goback() {
  router.go(-1)
}

function selectPlace(item) {
  // 更新Pinia store中的selectedPlaceName和currentAddress
  cityStore.setSelectedPlaceName(item.name)
  cityStore.updateCurrentAddress(item.name) // 更新当前地址
  router.push('/') // 跳转回home组件
}

onMounted(async () => {
  // 如果有传入的城市名，更新startCity并获取地点数据
  if (router.currentRoute.value.query.city) {
    await cityStore.updateStartCityAndRequest(router.currentRoute.value.query.city)
  }
})
</script>
<style scoped>
.upward img {
  margin-left: 0.27rem;
  width: 0.19rem;
  height: 0.32rem;
}
.upward {
  width: 7.5rem;
  height: 1.76rem;
}
.text {
  font-weight: 500;
  font-size: 0.34rem;
  text-align: center;
  margin-left: 3.07rem;
}
.content {
  width: 7.5rem;
  height: 1.26rem;
  background: #ffffff;
}
.box {
  display: flex;
  width: 6.7rem;
  height: 0.88rem;
  background: #f4f6f8;
  border-radius: 20px;
  margin-left: 0.4rem;
  align-items: center;
}
.box span {
  font-size: 0.32rem;
  width: 2.2rem;
  height: 0.7rem;
}
.green {
  width: 0.14rem;
  height: 0.14rem;
  margin-left: 0.1rem;
}
.tri {
  width: 0.18rem;
  height: 0.09rem;
  padding-left: 0.1rem;
}
.down {
  width: 100%;
  height: 123.3rem;
  background: #ffffff;
}
.area1 img {
  margin-left: 0.36rem;
  width: 0.34rem;
  height: 0.34rem;
}
.title {
  font-weight: 500;
  color: #333333;
  text-align: left;
  font-size: 0.28rem;
}
.gray {
  font-weight: 400;
  color: #999999;
  text-align: left;
  font-size: 0.26rem;
}
.downflex {
  display: flex;
  align-items: center;
  margin-bottom: 0.1rem;
}
.downtext {
  display: flex;
  flex-direction: column;
}
</style>
